/*
	This file is part of GTimer.

    GTimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License\.

    GTimer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GTimer. If not, see <http://www.gnu.org/licenses/>.
	
	Copyright 2012 Muhammad Ghareeb Sideek
	Contact: MGhareeb64@gmail.com
 */

package com.gsourcecode.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.text.InputFilter;
import android.text.Spanned;

public class GInputFilter implements InputFilter {

	Pattern mPattern;

	public GInputFilter(int digitsBeforeZero) {
		mPattern = Pattern.compile("[0-9]{0," + (digitsBeforeZero - 1) + "}");
	}

	public GInputFilter(String s) {
		mPattern = Pattern.compile(s + "*$");
	}

	@Override
	public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

		Matcher matcher = mPattern.matcher(dest);
		if (!matcher.matches())
			return "";
		return null;
	}

}
